<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>



    <script>
        function a(x) {
            return new Promise(function(resolve,y) {
                console.log(`在a中,a为${x}`);
                // setTimeout(resolve,500,x+x); //延迟0.5s传输x+x
                resolve(x+x); //传输x+x
            })
        }
        function b(x) {
            return new Promise(function(resolve,y) {
                console.log(`在b中,b为${x}`);
                // setTimeout(resolve,500,x*x); //延迟0.5s传输x*x
                resolve(x*x); //传输x*x
            })
        }


        var p1 = new Promise(function(resolve,reject){
            resolve(1); //定义开始计算的值
        })
        p1.then(a).then(b).then(a).then(function (x) { //执行 a-b-a
                console.log(x);
            });

        var c = x=>{
            console.log(x);
        }
        p1.then(a).catch(c); //如果a错误执行b

        Promise.all([a,b]).then(a); //全部正确执行，再执行接下的a

        Promise.race([a,b]).then(a); //逐个执行,一个对就执行接下的a

    </script>
</body>

</html>